Third Party Control Integration

Use Microsoft's Calendar Control to Pick Dates

Description
This example shows how to use a third-party control to edit a date field value.
Variables
Parent Table
Select a database table
Record Control Class
Select the record control where this customization will be inserted
Date Field Control
Select a date field that interacts with the calendar control
Applies to
RecordControl class
Code
 
  
/// 
/// Event handler for the init event
///  
private void myCalendarInit(object sender, System.EventArgs e)
{
    System.Web.UI.WebControls.Calendar calendar;
    calendar = ((System.Web.UI.WebControls.Calendar)(this.Page.FindControlRecursively("calendar")));
    calendar.SelectionChanged += new System.EventHandler(this.CalendarControl_SelectionChanged);
}


/// 
/// CalendarControl_SelectionChanged sets the text of the field value control
/// to the dated selected by the user
///  
private void CalendarControl_SelectionChanged(object sender, System.EventArgs e)
{
    System.Web.UI.WebControls.Calendar calendar;
    calendar = ((System.Web.UI.WebControls.Calendar)(this.Page.FindControlRecursively("calendar")));
    if (!(calendar == null))
    {
        ${Date Field Control}.Text = calendar.SelectedDate.ToString();
        ${Parent Table}Record rec = new ${Parent Table}Record();
        rec.Parse(calendar.SelectedDate.ToString(), ${${Parent Table}ClassName}.${Date Field Control});
        this.${Date Field Control}.Text = rec.Format(${${Parent Table}ClassName}.${Date Field Control});
    }
}


/// 
/// Override DataBind(), call base Databind() to populate the UI
/// controls using the DataSource 
///  
public override void DataBind()
{
    base.DataBind();
    if (!this.Page.IsPostBack)
    {
        if ((this.DataSource == null))
        {
            return;
        }
        ${Date Field Control}.Text  = this.DataSource.Format(${${Parent Table}ClassName}.${Date Field Control});            
        if (this.${Date Field Control}.Text.Length > 0)
        {
            DateTime dt = Convert.ToDateTime(this.${Date Field Control}.Text);
            System.Web.UI.WebControls.Calendar calendar;
            calendar = ((System.Web.UI.WebControls.Calendar)(this.Page.FindControlRecursively("calendar")));
            if (!(calendar == null)) 
            {
                calendar.SelectedDate = dt;
                calendar.VisibleDate = dt;
            }
        }
    }
}


/// 
/// Set the text of the ${Date Field Control} and then call
/// base.SaveData() to  save the data
///  
public override void SaveData()
{
    System.Web.UI.WebControls.Calendar calendar;
    calendar = ((System.Web.UI.WebControls.Calendar)(this.Page.FindControlRecursively("calendar")));
    if (!(calendar == null) && calendar.SelectedDate.Year!= 0001 && ${Date Field Control}.Text == calendar.SelectedDate.ToString())
    {
        ${Date Field Control}.Text  = calendar.SelectedDate.ToString();
    }

    // Call base.SaveData()
    base.SaveData();
}


	 
Applies to
CSharpRecordControlConstructor class
Code
 
	// The following line will be inserted inside the
	// constructor for page class
	this.Init += new System.EventHandler(myCalendarInit); 
	 

Terms of Service Privacy Statement